package com.example.nccdeploytool.util;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @author 15037
 * @description
 * @date 2025-07-10 10:18
 */
public class CompressUtil {

    public static void unzip(String zipFilePath, String destDir) throws IOException {
        File dir = new File(destDir);
        if (!dir.exists()) dir.mkdirs();

        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath))) {
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                String filePath = destDir + File.separator + entry.getName();
                String parent = new File(filePath).getParent();
                File fileParent = new File(parent);
                if (!fileParent.exists()) {
                    fileParent.mkdirs();
                }
                if (!entry.isDirectory()) {
                    extractFile(zis, filePath);
                }
                zis.closeEntry();
            }
        }
    }

    private static void extractFile(ZipInputStream zis, String filePath) throws IOException {
        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = zis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }
        }
    }

    public static void main(String[] args) {
        try {
            unzip("D:\\BIPOUT\\patch_1752111944409.zip", "D:\\BIPOUT\\output_folder");
            System.out.println("解压完成！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
